package gacl.javaweb10.httprequest;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zcx
 *
 * 2017年3月30日 下午2:09:38
 */
public class RequestDemo01 extends HttpServlet {
	
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
		/**
		 * 1.获得客户机信息
		 */
		String requestUrl = request.getRequestURL().toString();//得到请求的URL地址
		String requestUri = request.getRequestURI();//得到请求的资源
		String queryString = request.getQueryString();//得到请求的URL地址中附带的参数
		String remoteAddr = request.getRemoteAddr();//得到来访者的IP地址
		String remoteHost = request.getRemoteHost();
		int remotePort = request.getRemotePort();
		String remoteUser = request.getRemoteUser();
		String method = request.getMethod();//得到请求URL地址时使用的方法
		String pathInfo = request.getPathInfo();
		String localAddr = request.getLocalAddr();//获取WEB服务器的IP地址
		String localName = request.getLocalName();//获取WEB服务器的主机名
		response.setCharacterEncoding("UTF-8");//设置将字符以“UTF-8”编码输出到客户端浏览器
		//通过设置响应头控制浏览器以“UTF-8”的编码显示数据，如果不加这句话，那么浏览器显示的将是乱码
		response.setHeader("content-type",	"text/html;charset=UTF-8");
		PrintWriter printWriter_out = response.getWriter();
		printWriter_out.write("获取到的客户机信息如下：");
		printWriter_out.write("<hr/>");
		printWriter_out.write("请求的URL地址：" + requestUrl);
		printWriter_out.write("<br/>");
		printWriter_out.write("请求的资源：" + requestUri);
		printWriter_out.write("<br/>");
		printWriter_out.write("请求的URL地址中附带的参数：" + queryString);
		printWriter_out.write("<br/>");
		printWriter_out.write("来访者的IP地址：" + remoteAddr);
		printWriter_out.write("<br/>");
		printWriter_out.write("remoteUser:" + remoteUser);
		printWriter_out.write("<br/>");
		printWriter_out.write("请求使用的方法：" + method);
		printWriter_out.write("<br/>");
		printWriter_out.write("pathInfo:" + pathInfo);
		printWriter_out.write("<br/>");
		printWriter_out.write("localAddr:" + localAddr);
		printWriter_out.write("<br/>");
		printWriter_out.write("localName:" + localName);
		
		
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse reponse) throws ServletException, IOException{
		doGet(request, reponse);
	}
}
